<?php
/**
 * Created by PhpStorm.
 * Login: Administrator
 * Date: 2017-11-27
 * Time: 12:44
 */
namespace app\index\controller;

use think\Request;
use think\Db;

class Flow extends Base
{
    public function flowlist()
    {
        //流程列表
        $this->baseindex();
        $where_if = input('post.sousuo');
        $where_a = [];
        $where_res = '';
        $where = "f.branch_id=b.id and f.region_id=r.id and f.bill_type_id=t.id";
        if (isset($_POST['submit']) && $_POST['submit'] == 'submit') {
            //搜索功能
            $type_approve = Db::table('oa_type_approve')->field('id')
                ->where('type_approve', 'like', "%$where_if%")
                ->select();
            if (!empty($type_approve)) {
                //判断是不是搜索名称
                foreach ($type_approve as $key => $value) {
                    $where_a[] = $where . " and f.bill_type_id=" . $value['id'];
                }
                for ($i = 0; $i < count($where_a) - 1; $i++) {
                    $where_res .= $where_a[$i] . " or ";
                }
                $where = $where_res . $where_a[count($where_a) - 1];
                //dump($where);die;
            } else {
                //搜索审批流ID
                $where .= " and f.id=" . $where_if;
            }
        }
        $result = Db::table('oa_flow f,oa_type_approve t,oa_branch b,oa_region r')->field('f.id,f.first,f.second,f.third,f.fourth,
        f.fifth,f.sixth,f.seventh,f.eighth,f.ninth,f.tenth,r.region,b.branch,
        t.type_approve')->where($where)
            ->select();
        //dump($result);die;
        foreach ($result as $k => $v) {
            if ($v['first']) {
                $role_first = Db::table('oa_role')->where('id=' . $v['first'])->find();
                if (!empty($role_first)) {
                    $result[$k]['first'] = $role_first['role'];
                    $result[$k]['detials'] = "申请->" . $role_first['role'];
                }
            } //dump($result);die;
            if (!empty($v['second'])) {
                $role_second = Db::table('oa_role')->where('id=' . $v['second'])->find();
                if (!empty($role_second)) {
                    $result[$k]['second'] = $role_second['role'];
                    $result[$k]['detials'] = $result[$k]['detials'] . '->' . $role_second['role'];
                }
            }
            if (!empty($v['third'])) {
                $role_third = Db::table('oa_role')->where('id=' . $v['third'])->find();
                if (!empty($role_third)) {
                    $result[$k]['third'] = $role_third['role'];
                    $result[$k]['detials'] = $result[$k]['detials'] . "->" . $role_third['role'];
                }
            }
            if (!empty($v['fourth'])) {
                $role_fourth = Db::table('oa_role')->where('id=' . $v['fourth'])->find();
                if (!empty($role_fourth)) {
                    $result[$k]['fourth'] = $role_fourth['role'];
                    $result[$k]['detials'] = $result[$k]['detials'] . "->" . $role_fourth['role'];
                }
            }
            if (!empty($v['fifth'])) {
                $role_fifth = Db::table('oa_role')->where('id=' . $v['fifth'])->find();
                if (!empty($role_fifth)) {
                    $result[$k]['fifth'] = $role_fifth['role'];
                    $result[$k]['detials'] = $result[$k]['detials'] . "->" . $role_fifth['role'];
                }
            }
            if (!empty($v['sixth'])) {
                $role_sixth = Db::table('oa_role')->where('id=' . $v['sixth'])->find();
                if (!empty($role_sixth)) {
                    $result[$k]['sixth'] = $role_sixth['role'];
                    $result[$k]['detials'] = $result[$k]['detials'] . "->" . $role_sixth['role'];
                }
            }
            if (!empty($v['seventh'])) {
                $role_seventh = Db::table('oa_role')->where('id=' . $v['seventh'])->find();
                if (!empty($role_seventh)) {
                    $result[$k]['seventh'] = $role_seventh['role'];
                    $result[$k]['detials'] = $result[$k]['detials'] . "->" . $role_seventh['role'];
                }
            }
            if (!empty($v['eighth'])) {
                $role_eighth = Db::table('oa_role')->where('id=' . $v['eighth'])->find();
                if (!empty($role_eighth)) {
                    $result[$k]['eighth'] = $role_eighth['role'];
                    $result[$k]['detials'] = $result[$k]['detials'] . "->" . $role_eighth['role'];
                }
            }
            if (!empty($v['ninth'])) {
                $role_ninth = Db::table('oa_role')->where('id=' . $v['ninth'])->find();
                if (!empty($role_ninth)) {
                    $result[$k]['ninth'] = $role_ninth['role'];
                    $result[$k]['detials'] = $result[$k]['detials'] . "->" . $role_ninth['role'];
                }
            }
            if (!empty($v['tenth'])) {
                $role_tenth = Db::table('oa_role')->where('id=' . $v['tenth'])->find();
                if (!empty($role_tenth)) {
                    $result[$k]['tenth'] = $role_tenth['role'];
                    $result[$k]['detials'] = $result[$k]['detials'] . "->" . $role_tenth['role'];
                }
            }
        }
        //dump($result);die;
        $this->assign('list', $result);
        return $this->fetch();
    }

    public function flowadd()
    {
        //流程添加页面
        $this->baseindex();
        $type_approve = Db::table('oa_type_approve')->select();
        $this->assign('type_approve', $type_approve);

        return $this->fetch();
    }

    public function flowsave()
    {
        //流程修改页面
        $this->baseindex();
        if (isset($_GET['id'])) {
            $id = input('get.id');
            $role = Db::table('oa_role')->select();
            $results = [];
            $type_approve_id = Db::table('oa_flow f')->field('f.id,f.region_id,f.branch_id,
            f.bill_type_id,f.first,f.second,f.third,f.fourth,f.fifth,f.sixth,f.seventh,
            f.eighth,f.ninth,f.tenth,r.region,b.branch,t.type_approve')
                ->join('oa_region r', 'f.region_id=r.id', 'left')
                ->join('oa_branch b', 'f.branch_id=b.id', 'left')
                ->join('oa_type_approve t', 'f.bill_type_id=t.id', 'left')->where('f.id=' . $id)->find();
            if (!empty($type_approve_id['first'])) {
                $result['role'] = $role;
                $result['selected'] = $type_approve_id['first'];
                $results[] = $result;
            }
            if (!empty($type_approve_id['second'])) {
                $result['role'] = $role;
                $result['selected'] = $type_approve_id['second'];
                $results[] = $result;
            }
            if (!empty($type_approve_id['third'])) {
                $result['role'] = $role;
                $result['selected'] = $type_approve_id['third'];
                $results[] = $result;
            }
            if (!empty($type_approve_id['fourth'])) {
                $result['role'] = $role;
                $result['selected'] = $type_approve_id['fourth'];
                $results[] = $result;
            }
            if (!empty($type_approve_id['fifth'])) {
                $result['role'] = $role;
                $result['selected'] = $type_approve_id['fifth'];
                $results[] = $result;
            }
            if (!empty($type_approve_id['sixth'])) {
                $result['role'] = $role;
                $result['selected'] = $type_approve_id['sixth'];
                $results[] = $result;
            }
            if (!empty($type_approve_id['seventh'])) {
                $result['role'] = $role;
                $result['selected'] = $type_approve_id['seventh'];
                $results[] = $result;
            }
            if (!empty($type_approve_id['eighth'])) {
                $result['role'] = $role;
                $result['selected'] = $type_approve_id['eighth'];
                $results[] = $result;
            }
            if (!empty($type_approve_id['ninth'])) {
                $result['role'] = $role;
                $result['selected'] = $type_approve_id['ninth'];
                $results[] = $result;
            }
            if (!empty($type_approve_id['tenth'])) {
                $result['role'] = $role;
                $result['selected'] = $type_approve_id['tenth'];
                $results[] = $result;
            }
            //dump($results);die;
            $type_approve = Db::table('oa_type_approve')->select();
            $this->assign('type_approve', $type_approve);
            $this->assign('list', $type_approve_id);
            $this->assign('role', $results);
            return $this->fetch();
        }

    }

    public function flowadd_sub()
    {
        //流程添加
        $this->baseindex();
        if (isset($_POST['submit']) && $_POST['submit'] == 'submit') {
            $res = [];
            $res_json=input('post.data');
            $res_array_a=json_decode($res_json,true);
            $res_array_b=json_decode($res_json,true);
            unset($res_array_a['region_id']);
            unset($res_array_a['branch_id']);
            unset($res_array_a['type_approve']);
            //$arr = Request::instance()->except(['submit', 'region_id', 'branch_id', 'type_approve']);
            $arra = array_values($res_array_a);
            $num = count($arra);
            //dump($num);die;
            if ($num == 1) {
                $res['first'] = $arra[0];
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
            } elseif ($num == 2) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
            } elseif ($num == 3) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
            } elseif ($num == 4) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = $arra[3];
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
            } elseif ($num == 5) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = $arra[3];
                $res['fifth'] = $arra[4];
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
            } elseif ($num == 6) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = $arra[3];
                $res['fifth'] = $arra[4];
                $res['sixth'] = $arra[5];
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
            } elseif ($num == 7) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = $arra[3];
                $res['fifth'] = $arra[4];
                $res['sixth'] = $arra[5];
                $res['seventh'] = $arra[6];
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
            } elseif ($num == 8) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = $arra[3];
                $res['fifth'] = $arra[4];
                $res['sixth'] = $arra[5];
                $res['seventh'] = $arra[6];
                $res['eighth'] = $arra[7];
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
            } elseif ($num == 9) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = $arra[3];
                $res['fifth'] = $arra[4];
                $res['sixth'] = $arra[5];
                $res['seventh'] = $arra[6];
                $res['eighth'] = $arra[7];
                $res['ninth'] = $arra[8];
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
            } elseif ($num == 10) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = $arra[3];
                $res['fifth'] = $arra[4];
                $res['sixth'] = $arra[5];
                $res['seventh'] = $arra[6];
                $res['eighth'] = $arra[7];
                $res['ninth'] = $arra[8];
                $res['tenth'] = $arra[9];
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
            } else {
                return 2;
            }
            $result = Db::table('oa_flow')->insert($res);
            return $result;
        }
    }

    public function flowsave_sub()
    {
        //流程修改
        $this->baseindex();
        if (isset($_POST['submit']) && $_POST['submit'] == 'submit') {
            $res = [];
            $res_json=input('post.data');
            $res_array_a=json_decode($res_json,true);
            $res_array_b=json_decode($res_json,true);
            unset($res_array_a['region_id']);
            unset($res_array_a['branch_id']);
            unset($res_array_a['type_approve']);
            unset($res_array_a['id']);
            $arra = array_values($res_array_a);
            $num = count($arra);
            if ($num == 1) {
                $res['first'] = $arra[0];
                $res['second'] = '';
                $res['third'] = '';
                $res['fourth'] = '';
                $res['fifth'] = '';
                $res['sixth'] = '';
                $res['seventh'] = '';
                $res['eighth'] = '';
                $res['ninth'] = '';
                $res['tenth'] = '';
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
                $res['id'] = $res_array_b['id'];
            } elseif ($num == 2) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = '';
                $res['fourth'] = '';
                $res['fifth'] = '';
                $res['sixth'] = '';
                $res['seventh'] = '';
                $res['eighth'] = '';
                $res['ninth'] = '';
                $res['tenth'] = '';
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
                $res['id'] = $res_array_b['id'];
            } elseif ($num == 3) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = '';
                $res['fifth'] = '';
                $res['sixth'] = '';
                $res['seventh'] = '';
                $res['eighth'] = '';
                $res['ninth'] = '';
                $res['tenth'] = '';
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
                $res['id'] = $res_array_b['id'];
            } elseif ($num == 4) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = $arra[3];
                $res['fifth'] = '';
                $res['sixth'] = '';
                $res['seventh'] = '';
                $res['eighth'] = '';
                $res['ninth'] = '';
                $res['tenth'] = '';
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
                $res['id'] = $res_array_b['id'];
            } elseif ($num == 5) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = $arra[3];
                $res['fifth'] = $arra[4];
                $res['sixth'] = '';
                $res['seventh'] = '';
                $res['eighth'] = '';
                $res['ninth'] = '';
                $res['tenth'] = '';
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
                $res['id'] = $res_array_b['id'];
            } elseif ($num == 6) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = $arra[3];
                $res['fifth'] = $arra[4];
                $res['sixth'] = $arra[5];
                $res['seventh'] = '';
                $res['eighth'] = '';
                $res['ninth'] = '';
                $res['tenth'] = '';
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
                $res['id'] = $res_array_b['id'];
            } elseif ($num == 7) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = $arra[3];
                $res['fifth'] = $arra[4];
                $res['sixth'] = $arra[5];
                $res['seventh'] = $arra[6];
                $res['eighth'] = '';
                $res['ninth'] = '';
                $res['tenth'] = '';
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
                $res['id'] = $res_array_b['id'];
            } elseif ($num == 8) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = $arra[3];
                $res['fifth'] = $arra[4];
                $res['sixth'] = $arra[5];
                $res['seventh'] = $arra[6];
                $res['eighth'] = $arra[7];
                $res['ninth'] = '';
                $res['tenth'] = '';
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
                $res['id'] = $res_array_b['id'];
            } elseif ($num == 9) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = $arra[3];
                $res['fifth'] = $arra[4];
                $res['sixth'] = $arra[5];
                $res['seventh'] = $arra[6];
                $res['eighth'] = $arra[7];
                $res['ninth'] = $arra[8];
                $res['tenth'] = '';
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
                $res['id'] = $res_array_b['id'];
            } elseif ($num == 10) {
                $res['first'] = $arra[0];
                $res['second'] = $arra[1];
                $res['third'] = $arra[2];
                $res['fourth'] = $arra[3];
                $res['fifth'] = $arra[4];
                $res['sixth'] = $arra[5];
                $res['seventh'] = $arra[6];
                $res['eighth'] = $arra[7];
                $res['ninth'] = $arra[8];
                $res['tenth'] = $arra[9];
                $res['region_id'] = $res_array_b['region_id'];
                $res['branch_id'] = $res_array_b['branch_id'];
                $res['bill_type_id'] = $res_array_b['type_approve'];
                $res['id'] = $res_array_b['id'];
            } else {
                return 2;
            }
            //dump($res);die;
            $res_region=Db::table('oa_flow')->where($res)->find();
            if ($res_region){
                return 1;
            }else{
                $result = Db::table('oa_flow')->update($res);
                return $result;
            }

        }
    }

    public function flowdel()
    {
        $this->baseindex();
        if (isset($_POST['submit']) && $_POST['submit'] == "submit") {
            $id = input('post.id');
            $res = Db::table('oa_flow')->delete($id);
            return $res;
        }
    }
}